package lab.antlrtest;

import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

/**
 * play script, antlr 版
 */
public class PlayScript {
    public static void main(String[] args) {
        String script = "2+6/2 * 4";
        // 词法分析
        PlayScriptLexer lexer = new PlayScriptLexer(CharStreams.fromString(script));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);

        // 语法分析
        PlayScriptParser parser = new PlayScriptParser(tokenStream);
        ParseTree tree = parser.additiveExpression();

        // 打印语法树
        System.out.println("the lisp style ast of: " + script);
        System.out.println(tree.toStringTree(parser));

        // 解释执行
        ASTEvaluator astEvaluator = new ASTEvaluator();
        Integer result = astEvaluator.visit(tree);
        System.out.println("value of : "+script+" is " + result);
    }
}
